package com.power.services.district;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.power.dao.district.TsDistrictDAO;
import com.power.model.district.TsDistrict;
import com.power.util.Assent;

@Service("tsDistrictService")
public class TsDistrictServiceImpl implements TsDistrictService {

	@Autowired
	private TsDistrictDAO dao;

	public void create(TsDistrict arg0) {
		dao.save(arg0);
	}

	public void delete(TsDistrict arg0) {
		dao.delete(arg0);
	}

	public TsDistrict read(Integer arg0) {
		return dao.get(arg0);
	}

	public void update(TsDistrict arg0) {
		dao.update(arg0);
	}

	@SuppressWarnings("unchecked")
	public Map<String, Object> findAll(String disName, int start, int limit, Integer pid) {
		List<TsDistrict> disList = null;
		Map<String, Object> map = new HashMap<String, Object>();
		DetachedCriteria criteria = DetachedCriteria.forClass(TsDistrict.class);
		if(!Assent.checkNullAndEmpty(disName))
			criteria.add(Restrictions.like("name", "%"+disName+"%"));
		criteria.add(Restrictions.eq("upperId", pid));
		disList = dao.findByCriteria(criteria, start, limit);
		int count = dao.getRowCount(criteria);
		if(null == disList)
			disList = new ArrayList<TsDistrict>();
		map.put("data", disList);
		map.put("count", count);
		return map;
	}

	public Integer saveOrUpdate(TsDistrict district) {
		String sql = "select count(*) from t_s_district where (code='"+district.getCode()+"' or name='"+district.getName()+"')";
		if(null!=district.getId())
			sql+=" and id!="+district.getId();
		if(Integer.valueOf(dao.findBySQL(sql).get(0).toString())>0){
			return 1;
		}
		dao.saveOrUpdate(district);
		return 0;
	}

	public void delete(String ids) throws Exception {
		String[] idAry = ids.split("_");
		for (int i = 0; i < idAry.length; i++) {
			Integer id = new Integer(idAry[i]);
			DetachedCriteria criteria = DetachedCriteria.forClass(TsDistrict.class);
			criteria.add(Restrictions.eq("upperId", id));
			int count = dao.getRowCount(criteria);
			if(count > 0){
				throw new Exception("1");
			}
			dao.deleteByKey(id);
		}
	}

	@SuppressWarnings("unchecked")
	public List<TsDistrict> loadByPid(Integer pid) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TsDistrict.class);
		criteria.add(Restrictions.eq("upperId", pid));
		return dao.findByCriteria(criteria);
	}
 
	@SuppressWarnings("unchecked")
	public List<TsDistrict> findAllPro() {
		List<TsDistrict> disList = null;
		DetachedCriteria criteria = DetachedCriteria.forClass(TsDistrict.class);
		criteria.add(Restrictions.eq("upperId", -1));
		disList = dao.findByCriteria(criteria);
		if(null == disList)
			disList = new ArrayList<TsDistrict>();
		return disList;
	}
	
	@SuppressWarnings("unchecked")
	public List<TsDistrict> findAllCityByProId1(Integer id) {
		return dao.find("SELECT t FROM TsDistrict t ,TsDistrict a WHERE t.upperId =  a.upperId  AND a.id='"+ id + "'");
	}
	
	@SuppressWarnings("unchecked")
	public List<TsDistrict> findAllCityByProId2(Integer id) {
		return dao.find("SELECT t FROM TsDistrict t WHERE t.upperId ='"+ id + "'");
	}

	@SuppressWarnings("unchecked")
	public Map<String, List<TsDistrict>> findAllDis() {
		DetachedCriteria criteria = DetachedCriteria.forClass(TsDistrict.class);
		criteria.add(Restrictions.ne("upperId", -1));
		List<TsDistrict> list = dao.findByCriteria(criteria);
		Map<String, List<TsDistrict>> map = new HashMap<String, List<TsDistrict>>();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			TsDistrict district = (TsDistrict) iterator.next();
			List<TsDistrict> dataList;
			String key = district.getFirstChar();
			if(null == map.get(key)){
				dataList = new ArrayList<TsDistrict>();
				dataList.add(district);
			}else{
				dataList = map.get(key);
				dataList.add(district);
			}
			map.put(key, dataList);
		}
		map = new TreeMap(map);
		return map;
	}
}
